package com.syq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syq.mapper.DtsCategoryMapper;
import com.syq.pojo.DtsCategory;
import com.syq.service.DtsCategoryService;
import com.syq.util.ResponseUtil;
import com.syq.vo.CatVo;
import com.syq.vo.CategorySellAmts;
import com.syq.vo.CategorySellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CategoryServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/22 14:32
 */

@Service
@Transactional
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;



    @Override
    public List<CategorySellAmts> findCategorySellAmts() {
        return dtsCategoryMapper.findCategorySellAmts();
    }

    @Override
    public Integer findById(Integer categoryId) {
        return dtsCategoryMapper.selectById(categoryId).getPid();
    }

    @Override
    public List<CatVo> findPid() {
        return dtsCategoryMapper.selectByPid0();
    }

    @Override
    public Object findAll(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        Map<String,Object> map=new HashMap<>();
        Page<DtsCategory> dtsCategoryPage = dtsCategoryMapper.selectPage(new Page<>(page, limit),
                new QueryWrapper<DtsCategory>()
                        .eq("deleted", false)
                        .like(!StringUtils.isEmpty(name), "name", name)
                        .eq(!StringUtils.isEmpty(id), "id", id)
                        .orderBy(!StringUtils.isEmpty(sort), "Asc".equalsIgnoreCase(order), sort));
        map.put("total",dtsCategoryPage.getTotal());
        map.put("items",dtsCategoryPage.getRecords());
        return ResponseUtil.ok(map);
    }

    @Override
    public Object findL1() {
        List<CatVo> l1List = dtsCategoryMapper.selectByPid(0);
        return ResponseUtil.ok(l1List);
    }

    @Override
    public Object add(DtsCategory category) {
        if ("L1".equals(category.getLevel())){
            category.setPid(0);
        }
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        dtsCategoryMapper.insert(category);
        DtsCategory dtsCategory = dtsCategoryMapper.selectById(category.getId());
        return ResponseUtil.ok(dtsCategory);
    }

    @Override
    public Object update(DtsCategory category) {
        if ("L1".equals(category.getLevel())){
            category.setPid(0);
        }
        category.setUpdateTime(new Date());
        dtsCategoryMapper.update(category,new QueryWrapper<DtsCategory>().eq("id",category.getId()));
        return ResponseUtil.ok();
    }

    @Override
    public Object delete(DtsCategory category) {
        dtsCategoryMapper.setDeleted(category.getId());
        return ResponseUtil.ok();
    }


}
